<!--
 * @author wangpeizhi
 闯关管理
 -->
<template>
  <div class="rush-container">
    <el-collapse-transition>
      <div class="course-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model="listQuery.anyThing"
          clearable
          placeholder="按闯关名称进行搜索"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <div class="course-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter"
            >搜索</el-button
          >
          <el-button class="filter-item" type="primary" @click="handleReset"
            >重置</el-button
          >
        </div>
      </div>
    </el-collapse-transition>
    <div class="course-btn">
      <div class="btn-title">
        <span>闯关管理</span>
      </div>
      <div class="btn-btns">
        <el-button style="margin-left: 10px" @click="handleCreate"
          >新建</el-button
        >
      </div>
    </div>
    <div v-loading="listLoading" class="course-list">
      <el-row>
        <el-col v-for="(item, index) in rushList" :key="item.id" :span="5">
          <div class="course-list-item">
            <div :class="sidebar.opened ? 'o' : 'c'" class="item-img">
              <img :src="filterRushImg(item.dailyImg, index)" alt class="bg" />
              <img
                v-if="view + item.dailyImg"
                :src="view + item.dailyImg"
                class="item-bg-img"
                :onerror="defaultRushImg"
              />
              <div class="title etc">{{ item.dailyName || "" }}</div>
              <div v-if="item.status == 'BDZT02'" class="pub">
                <span>禁用</span>
              </div>
            </div>
            <p class="item-mark etc">
              试题数量：{{ item.questionNumber || "" }}
            </p>
            <p class="item-mark etc">
              题库：{{ item.questionIdValue || "未设置" }}
            </p>
            <div class="item-btns">
              <el-button type="text" @click="handleDelete(item)"
                >删除</el-button
              >
              <el-button type="text" @click="handleEdit(item)">编辑</el-button>
              <el-button
                type="text"
                :disabled="!item.questionIdValue"
                size="mini"
                @click="handleStatus(item)"
                >{{ item.status == "BDZT01" ? "禁用" : "启用" }}</el-button
              >
            </div>
            <div class="item-btns-bottom">
              <el-button type="text" @click="handleBank(item)"
                >设置题库</el-button
              >
              <el-button type="text" @click="handleAddress(item.id)"
                >查看地址</el-button
              >
            </div>
          </div>
        </el-col>
        <div v-if="rushList.length <= 0" class="nodata">
          <img src="../images/nodata.png" />
        </div>
      </el-row>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <rush-bank v-if="showbank" :id="vid" type="CGLX01" @cancel="handleCancel" />
  </div>
</template>
<script>
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
import RushBank from "@/components/Bank";
import { queryPage, update, del } from "./../api/rush";
import { mapGetters } from "vuex";
import rushOne from "./../images/rush-one.png";
import rushTwo from "./../images/rush-two.png";
import rushThree from "./../images/rush-three.png";
import rushFour from "./../images/rush-four.png";
import defaultRushImg from "./../images/rush-default.png";
import rushFive from "./../images/rush-five.png";
import rushSix from "./../images/rush-six.png";
import rushSeven from "./../images/rush-seven.png";
import rushEight from "./../images/rush-eight.png";
import permission from "@/directive/permission/index.js"; // 权限判断指令

export default {
  name: "RushIndex",
  components: { Pagination, RushBank },
  directives: { permission },
  data() {
    return {
      defaultRushImg: 'this.src="' + defaultRushImg + '"',
      listLoading: false,
      list: null,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        anyThing: "",
      },
      checkList: [],
      rushList: [],
      view: "/API-DOCUMENT/document/file/download?id=",
      vid: "",
      showbank: false,
      link: "/rush/answer?id=",
    };
  },
  computed: {
    ...mapGetters(["sidebar"]),
  },
  created() {
    this.getList();
  },
  methods: {
    /**
     * 格式化闯关背景图片
     * @param index
     * @returns {*}
     */
    filterRushImg(imgId, index) {
      if (imgId) {
        if (index % 4 === 0) {
          return rushFive;
        }
        if (index % 4 === 1) {
          return rushSix;
        }
        if (index % 4 === 2) {
          return rushSeven;
        }
        if (index % 4 === 3) {
          return rushEight;
        }
      } else {
        if (index % 4 === 0) {
          return rushOne;
        }
        if (index % 4 === 1) {
          return rushTwo;
        }
        if (index % 4 === 2) {
          return rushThree;
        }
        if (index % 4 === 3) {
          return rushFour;
        }
      }
    },

    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },

    handleReset() {
      this.listQuery.anyThing = "";
      this.listQuery.page = 1;
      this.getList();
    },

    handleClassify() {
      this.classifyDialogVisible = true;
    },

    handleCreate() {
      this.$router.push({
        path: this.$filterPath("manage"),
        query: { type: "c" },
      });
    },

    handleEdit(row) {
      this.$router.push({
        path: this.$filterPath("manage"),
        query: { type: "u", i: row.id },
      });
    },

    handleStatus(row) {
      const state = row.status === "BDZT01" ? "BDZT02" : "BDZT01";
      let msg = "";
      if (row.status === "BDZT01") {
        msg = "禁用";
      } else {
        msg = "启用";
      }
      this.$confirm("确定" + msg + "该闯关吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: "请稍后",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.3)",
          });
          return new Promise((resolve, reject) => {
            update({ id: row.id, status: state })
              .then(() => {
                loading.close();
                this.$message({
                  title: "成功",
                  message: msg + "成功",
                  type: "success",
                  duration: 2000,
                });
                this.getList();
              })
              .catch((error) => {
                loading.close();
                reject(error);
              });
          });
        })
        .catch(() => {});
    },

    handleDelete(row) {
      this.$confirm("此操作将永久删除该闯关, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: "请稍后",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.3)",
          });
          return new Promise((resolve, reject) => {
            del(row.id)
              .then(() => {
                loading.close();
                this.$message({
                  title: "成功",
                  message: "删除成功",
                  type: "success",
                  duration: 2000,
                });
                this.getList();
              })
              .catch((error) => {
                loading.close();
                reject(error);
              });
          });
        })
        .catch();
    },
    /**
     * 查看前端地址
     * /rush/answer?id=Zffd5f2cd-8c3d-459d-8c8b-e4b30b6c6212
     */
    handleAddress(id) {
      this.$confirm(
        `链接地址为：${this.link}${id}<span style="color:red">注意：该链接只对已发布数据有效</span>`,
        "提示",
        {
          dangerouslyUseHTMLString: true,
          confirmButtonText: "复制链接",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          const input = document.createElement("input");
          input.setAttribute("readonly", "readonly");
          input.setAttribute("value", this.link + id);
          document.body.appendChild(input);
          input.select();
          if (document.execCommand("copy")) {
            document.execCommand("copy");
            this.$message({
              title: "成功",
              message: "复制成功！",
              type: "success",
              duration: 1500,
            });
          }
          document.body.removeChild(input);
        })
        .catch(() => {});
    },
    handleBank(row) {
      this.vid = row.id;
      this.showbank = true;
    },

    handleCancel() {
      this.showbank = false;
      this.getList();
    },

    getList(obj) {
      this.listLoading = true;
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        dailyName: this.listQuery.anyThing,
      };
      return new Promise((resolve, reject) => {
        queryPage(query)
          .then((response) => {
            this.listLoading = false;
            this.rushList = response.data.data.rows;
            this.total = response.data.data.total;
          })
          .catch((error) => {
            this.listLoading = false;
            reject(error);
          });
      });
    },
  },
};
</script>
<style rel="stylesheet/scss" lang="scss">
$dark_gray: #bfced6;
.rush-container {
  padding: 10px;
  .course-search {
    background-color: #fff;
    padding: 0 20px;
    .svg-container {
      padding: 0;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: "";
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .course-search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .course-search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }
    &.merge {
      height: 50px;
      line-height: 50px;
    }
  }
  .course-btn {
    display: flex;
    height: 36px;
    line-height: 36px;
    margin-bottom: 10px;
    margin-top: 10px;
    .btn-title {
      flex: 1;
      font-size: 16px;
      font-weight: bold;
      font-family: PingFangSC-Medium;
      color: rgba(51, 51, 51, 1);
    }
  }
  .course-list {
    min-height: 300px;
    .el-row {
      .el-col-5 {
        width: 20%;
      }
    }
    .course-list-item {
      background-color: #ffffff;
      padding: 10px 10px 0;
      margin-right: 10px;
      margin-bottom: 10px;
      box-shadow: 0 5px 10px rgba(254, 90, 97, 0.05);
      position: relative;

      .item-img {
        width: 100%;
        height: 150px;
        overflow: hidden;
        position: relative;
        .title {
          width: 60%;
          font-size: 12px;
          color: #fff;
          position: absolute;
          top: 10px;
          left: 10px;
        }
        .bg {
          position: absolute;
          width: 100%;
          height: 100%;
          z-index: 0;
          transition: all 0.2s linear;
          &:hover {
            transform: scale(1.1, 1.1);
            filter: contrast(130%);
          }
        }
        .item-bg-img {
          position: absolute;
          right: 10px;
          bottom: 10px;
          width: 60px;
          height: 60px;
          border-radius: 50%;
        }
        &.o {
          height: 130px;
        }
        .pub {
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background: rgba(0, 0, 0, 1);
          opacity: 0.5;
          z-index: 1;
          span {
            font-size: 12px;
            color: #ffffff;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
          }
        }
      }
      .item-mark {
        font-size: 12px;
        margin: 5px 0;
        line-height: 1.5;
        font-family: PingFangSC-Regular;
        font-weight: 400;
        color: rgba(166, 166, 166, 1);
      }
      .item-type {
        height: 20px;
        line-height: 20px;
        .type-gate {
          margin-right: 5px;
          img {
            width: 40px;
          }
        }
        .type-exam {
          img {
            width: 40px;
          }
        }
        .type-num {
          color: #333333;
          font-weight: 400;
          font-size: 12px;
          float: right;
        }
      }
      .item-btns-bottom {
        display: flex;
        justify-content: space-around;
        position: relative;
        .el-button {
          //flex: 1;
          padding-bottom: 10px;
          padding-top: 0;
          span {
            font-size: 13px;
            font-weight: 500;
          }
        }
      }
      .item-btns {
        display: flex;
        margin-top: 10px;
        position: relative;
        justify-content: space-around;
        .el-button {
          //flex: 1;
          span {
            font-size: 13px;
            font-weight: 500;
          }
        }
        &:before {
          content: "";
          position: absolute;
          top: 0;
          left: -10px;
          right: -10px;
          height: 1px;
          background-color: #e2e3e8;
        }
      }
    }
  }
  .pagination-container {
    background: none;
  }
}
</style>
